<?php

include("config.php");
if (!isset($_SESSION["user"]) || $_SESSION["user"] == "" || $kreditas != "1") header('Location: index.php');
include("header.php");
include("menu.php");

$page_size = "500";

function print_pages2($page,$page_size,$record_count,$cards){
	$cnt=1;
	$first_card="";
	$last_card="";
	$p = 1;
	if ($record_count > $page_size) {
		foreach ($cards as $card) {
			if ($first_card == "") {
				$first_card = trim($card["N51_PAV"]);
			}
			$last_card=trim($card["N51_PAV"]);
			if ($cnt >= $page_size) {
				if ($page == $p) {
					$cur="b";
				} else {
					$cur="";
				}
				$cnt = 0;
				echo "<A href='cards.php?p=".$p."' class='card'><span class='h20".$cur."'>" .$first_card . " - " . $last_card . "</span></A><br>";
				$first_card="";
				$p++;
			}
			$cnt++;
		}
		if ($cnt != "1") {
			if ($page == $p) {
				$cur="b";
			} else {
				$cur="";
			}		
			echo "<A href='cards.php?p=".$p."' class='card'><span class='h20".$cur."'>" .$first_card . " - " . $last_card . "</span></A><br>";
		}
	}
}

if(!isset($_REQUEST['p']) || !is_numeric($_REQUEST['p'])){
	$page = 1;
} else {
	$page = $_REQUEST['p'];
}
$db_www = new DB($server_username,$server_pass,$server_db,$server,$driver);
if ($db_www->isConnected()) {	
	$i_query = str_replace("<kodas>", $_SESSION['user'], $i_query);
	$minfo = $db_www->returnInfo($i_query);	

	
} else {
	die("Klientų aptarnavimo sistema laikinai neveikia.");
}
$db_rdata = new DB($rdata_server_username,$rdata_server_pass,$rdata_server_db,$rdata_server,$rdata_driver);
if (!$db_rdata->isConnected()) {	
	die("Klientų aptarnavimo sistema laikinai neveikia(rdata).");
}

if (isset($_REQUEST["act"])){
	if ($_REQUEST["act"] == "change" && isset($_REQUEST["descr"]) && isset($_REQUEST["nr"])) {
		$kchange_query = str_replace("<kodas>", $minfo["N08_kodas_ks"], $kchange_query);
		$kchange_query = str_replace("<pastabos>", @iconv('UTF-8','windows-1257',addslashes_mssql($_REQUEST["descr"])), $kchange_query);
		$kchange_query = str_replace("<kortele>", @iconv('UTF-8','windows-1257',addslashes_mssql($_REQUEST["nr"])), $kchange_query);

		$kchange_query_rdata = str_replace("<kortele>", @iconv('UTF-8','windows-1257',addslashes_mssql($_REQUEST["nr"])), $kchange_query_rdata);
		$kchange_query_rdata = str_replace("<nuo>", @iconv('UTF-8','windows-1257',addslashes_mssql($_REQUEST["nuo"])), $kchange_query_rdata);
		$kchange_query_rdata = str_replace("<iki>", @iconv('UTF-8','windows-1257',addslashes_mssql($_REQUEST["iki"])), $kchange_query_rdata);
		$kchange_query_rdata = str_replace("<credit>", @iconv('UTF-8','windows-1257',addslashes_mssql($_REQUEST["credit"])), $kchange_query_rdata);
		$kchange_query_rdata = str_replace("<p1>", @iconv('UTF-8','windows-1257',addslashes_mssql($_REQUEST["p1"])), $kchange_query_rdata);
		$kchange_query_rdata = str_replace("<p2>", @iconv('UTF-8','windows-1257',addslashes_mssql($_REQUEST["p2"])), $kchange_query_rdata);
		$kchange_query_rdata = str_replace("<p3>", @iconv('UTF-8','windows-1257',addslashes_mssql($_REQUEST["p3"])), $kchange_query_rdata);
		$kchange_query_rdata = str_replace("<p4>", @iconv('UTF-8','windows-1257',addslashes_mssql($_REQUEST["p4"])), $kchange_query_rdata);
		$kchange_query_rdata = str_replace("<p5>", @iconv('UTF-8','windows-1257',addslashes_mssql($_REQUEST["p5"])), $kchange_query_rdata);
		$kchange_query_rdata = str_replace("<p6>", @iconv('UTF-8','windows-1257',addslashes_mssql($_REQUEST["p6"])), $kchange_query_rdata);
		$kchange_query_rdata = str_replace("<p7>", @iconv('UTF-8','windows-1257',addslashes_mssql($_REQUEST["p7"])), $kchange_query_rdata);
		$kchange_query_rdata = str_replace("<lustas>", "NULL", $kchange_query_rdata);

		$db_www->execute($kchange_query);
		$db_rdata->execute($kchange_query_rdata);
		logAction("Klientas koreguoja:" . $_REQUEST["nr"],3);
	} else if ($_REQUEST["act"] == "block" && isset($_REQUEST["nr"])) {
		
		$kblock_query = str_replace("<kodas>", $minfo["N08_kodas_ks"], $kblock_query);
		$kblock_query = str_replace("<block>", "BLOCK", $kblock_query);
		$kblock_query = str_replace("<kortele>", @iconv('UTF-8','windows-1257',addslashes_mssql($_REQUEST["nr"])), $kblock_query);
		$db_www->execute($kblock_query);
		logAction("Klientas užblokuoja:" . $_REQUEST["nr"],2);
	} else if ($_REQUEST["act"] == "unblock" && isset($_REQUEST["nr"])) {
		
		$kblock_query = str_replace("<kodas>", $minfo["N08_kodas_ks"], $kblock_query);
		$kblock_query = str_replace("<block>", "UNBLOCK", $kblock_query);
		$kblock_query = str_replace("<kortele>", @iconv('UTF-8','windows-1257',addslashes_mssql($_REQUEST["nr"])), $kblock_query);
		
		$db_www->execute($kblock_query);
		logAction("Klientas atblokuoja:" . $_REQUEST["nr"],2);	
	}
}	
	if (!empty($minfo)) {
		$k_query = str_replace("<kodas>", $minfo["N08_kodas_ks"], $k_query);
		$cards = $db_www->returnList($k_query);	
		//echo $k_query;
	} else {
		die("Nerasta vartotojo įmonė.");
	}

?>
<!-- IE specific code: -->
<!--[if lte IE 9]> 
<style type="text/css"> 
#aep_ovrl { 
position: absolute; 
filter:alpha(opacity=70); 
top: expression(eval(document.body.scrollTop)); 
width: expression(eval(document.body.clientWidth)); 
} 
#aep_ww {  
position: absolute;  
top: expression(eval(document.body.scrollTop));  
} 
</style> 
<![endif]-->
<!-- ae_prompt HTML code -->
<div id="aep_ovrl" style="display: none;"></div>
<div id="aep_ww" style="display: none;">
<div id="aep_win"><div id="aep_t"></div>
<div id="aep_w"><div id="aep_center"><span id="aep_prompt"></span>
<table cellspacing="0" cellpadding="2" border="0" width="310">
<tr><td style="text-align:right;">Pavadinimas:</td><td style="text-align:right;" colspan="2"><input style="width:100%;" type="text" id="aep_text" onKeyPress=
"if((event.keyCode==10)||(event.keyCode==13)) ae_clk(1); if (event.keyCode==27) ae_clk(0);"></td></tr>
<tr><td style="text-align:right;">PIN:</td><td style="text-align:left;"><input MAXLENGTH=4 type="text" id="aep_pin" style="width:50px;" onKeyPress=
"if((event.keyCode==10)||(event.keyCode==13)) ae_clk(1); if (event.keyCode==27) ae_clk(0);"></td><td style="text-align:right;">
 Mėnesio kredito limitas: <input MAXLENGTH=6 style="width:50px;"  type="text" id="aep_credit" onKeyPress=
"if((event.keyCode==10)||(event.keyCode==13)) ae_clk(1); if (event.keyCode==27) ae_clk(0);"></td></tr>
<tr><td colspan="3" style="text-align:right;">
Leidžiama naudotis nuo val.: 

<input maxlength="2" type="text" id="aep_nuo" style="width:40px;">
 iki val.: <input maxlength="2" type="text" id="aep_iki" style="width:40px;">
</td></tr>
</table>
<div style="text-align:left;">
Kortelės neaptarnauti savaitės dienomis:

<table cellpadding="0" cellspacing="0" border="0" style="border-collapse:collpase;width:310px;border: 1px solid #c2c2c2;">
<tr><th width="50">I</th><th width="50">II</th><th width="50">III</th><th width="50">IV</th><th width="50">V</th><th width="50">VI</th><th width="50">VII</th></tr>
<tr>
<td style="text-align:center;"><input type="checkbox" id="aep_p1"></td>
<td style="text-align:center;"><input type="checkbox" id="aep_p2"></td>
<td style="text-align:center;"><input type="checkbox" id="aep_p3"></td>
<td style="text-align:center;"><input type="checkbox" id="aep_p4"></td>
<td style="text-align:center;"><input type="checkbox" id="aep_p5"></td>
<td style="text-align:center;"><input type="checkbox" id="aep_p6"></td>
<td style="text-align:center;"><input type="checkbox" id="aep_p7"></td>
</tr>
</table>
</div>
<br>
<table cellpadding="0" cellspacing="0" border="0" style="width:310px;">
<tr><td>
<input class="but bgfilter" type="button" id="aep_ok" onclick="ae_clk(1);" value="Gerai">
</td><td align="right">
<input class="but bgfilter" type="button" id="aep_cancel" onclick="ae_clk(0);" value="Atšaukti">
</td></tr></table>
</div>
</div>
</div>
</div>
<!-- ae_prompt HTML code -->

<SCRIPT language="JavaScript">
<!--
var card = null;
function changeDesc(nr,val) {
    card = nr;
	idx = val.indexOf(";");
	if (idx >= 0) {
		v1 = val.substring(0,idx);
		v2 = val.substring(idx+1);
	} else {
		v1 = "";
		v2 = val;
	}
	
var xmlhttp;
var txt,x,i;
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    xmlDoc=xmlhttp.responseXML;
	
 	nuo = '';
	iki = '';
	credit = '';
	p1 = '';
	p2 = '';
	p3 = '';
	p4 = '';
	p5 = '';
	p6 = '';
	p7 = '';    
    x=xmlDoc.getElementsByTagName("credit");	
    for (i=0;i<x.length;i++)
       if (x[i].childNodes.length >0) credit=x[i].childNodes[0].nodeValue;
      
    x=xmlDoc.getElementsByTagName("nuo");	
    for (i=0;i<x.length;i++)
       if (x[i].childNodes.length >0) nuo=x[i].childNodes[0].nodeValue.trim();

    x=xmlDoc.getElementsByTagName("iki");	
    for (i=0;i<x.length;i++)
       if (x[i].childNodes.length >0) iki=x[i].childNodes[0].nodeValue.trim();
	   
    x=xmlDoc.getElementsByTagName("p1");	
    for (i=0;i<x.length;i++)
       if (x[i].childNodes.length >0) p1=x[i].childNodes[0].nodeValue;

    x=xmlDoc.getElementsByTagName("p2");	
    for (i=0;i<x.length;i++)
       if (x[i].childNodes.length >0) p2=x[i].childNodes[0].nodeValue;

    x=xmlDoc.getElementsByTagName("p3");	
    for (i=0;i<x.length;i++)
       if (x[i].childNodes.length >0) p3=x[i].childNodes[0].nodeValue;

    x=xmlDoc.getElementsByTagName("p4");	
    for (i=0;i<x.length;i++)
       if (x[i].childNodes.length >0) p4=x[i].childNodes[0].nodeValue;

    x=xmlDoc.getElementsByTagName("p5");	
    for (i=0;i<x.length;i++)
       if (x[i].childNodes.length >0) p5=x[i].childNodes[0].nodeValue;

    x=xmlDoc.getElementsByTagName("p6");	
    for (i=0;i<x.length;i++)
       if (x[i].childNodes.length >0) p6=x[i].childNodes[0].nodeValue;

    x=xmlDoc.getElementsByTagName("p7");	
    for (i=0;i<x.length;i++)
       if (x[i].childNodes.length >0) p7=x[i].childNodes[0].nodeValue;	   
	   
		ae_prompt( hw2, 'Kortelės koregavimas', v1,v2,nuo,iki,credit,p1,p2,p3,p4,p5,p6,p7);   
    }
  }
  
xmlhttp.open("GET","card_info.php?card="+nr,true);
xmlhttp.send();	

}

 
function hw2(n,credit,nuo,iki,p1,p2,p3,p4,p5,p6,p7)
{
    if (n != null){		
		window.location='cards.php?act=change&nr='+card +'&descr='+n+'&credit='+credit+'&nuo='+nuo+'&iki='+iki+'&p1='+p1+'&p2='+p2+'&p3='+p3+'&p4='+p4+'&p5='+p5+'&p6='+p6+'&p7='+p7;
	}
}

// This is variable for storing callback function 
var ae_cb = null;

 
// this is a simple function-shortcut 
// to avoid using lengthy document.getElementById 
function ae$(a) { return document.getElementById(a); } 
 
// This is a main ae_prompt function 
// it saves function callback  
// and sets up dialog 
function ae_prompt(cb, q, b,a,nuo,iki,credit,p1,p2,p3,p4,p5,p6,p7) { 
	ae_cb = cb;
	ae$('aep_t').innerHTML = document.domain + ' klausimas:';
	ae$('aep_prompt').innerHTML = q;
	ae$('aep_text').value = a;
	ae$('aep_pin').value = b;
	ae$('aep_nuo').value = nuo;
	ae$('aep_iki').value = iki;
	ae$('aep_credit').value = credit;
	if (p1 == "1") ae$('aep_p1').checked = true; else ae$('aep_p1').checked = false;
	if (p2 == "1") ae$('aep_p2').checked = true; else ae$('aep_p2').checked = false;
	if (p3 == "1") ae$('aep_p3').checked = true; else ae$('aep_p3').checked = false;
	if (p4 == "1") ae$('aep_p4').checked = true; else ae$('aep_p4').checked = false;
	if (p5 == "1") ae$('aep_p5').checked = true; else ae$('aep_p5').checked = false;
	if (p6 == "1") ae$('aep_p6').checked = true; else ae$('aep_p6').checked = false;
	if (p7 == "1") ae$('aep_p7').checked = true; else ae$('aep_p7').checked = false;
	ae$('aep_ovrl').style.display = ae$('aep_ww').style.display = '';
	ae$('aep_text').focus();
	ae$('aep_text').select();
} 
 
// This function is called when user presses OK(m=0) or Cancel(m=1) button 
// in the dialog. You should not call this function directly. 
function ae_clk(m) { 
	if (!m)  
		ae_cb(null,null);  // user pressed cancel, call callback with null 
	else {
	    pin = ae$('aep_pin').value;
		if (!isUnsignedInteger(pin)) {
			alert("PIN kodas turi būti skaičius");
			return;
		}
		credit = ae$('aep_credit').value.trim();
		if (credit != "") {
			if (!isUnsignedInteger(credit)) {
				alert("Neteisingas kredito limitas");
				return;
			}
		}
		if (pin.length == 0) pin = "0000";
		else if (pin.length == 1) pin = "000" + pin;
		else if (pin.length == 2) pin = "00" + pin;
		else if (pin.length == 3) pin = "0" + pin;
		
		nuo =	ae$('aep_nuo').value;
		iki = ae$('aep_iki').value;
		if (nuo != "") {
			if (!isUnsignedInteger(nuo)) {
				alert("Nuo valanda turi būti 0-24");
				return;
			} else {
				p = parseInt(nuo);
				if (p > 24 || p < 0){
					alert("Nuo valanda turi būti 0-24");
					return;				
				}
			}
		}
		if (iki != "") {
			if (!isUnsignedInteger(iki)) {
				alert("Iki valanda turi būti 0-24");
				return;
			} else {
				p = parseInt(iki);
				if (p > 24 || p < 0){
					alert("Iki valanda turi būti 0-24");
					return;				
				}
			}
		}		
		
		
		if (ae$('aep_p1').checked == true) p1 = "1"; else p1 = "0";
		if (ae$('aep_p2').checked == true) p2 = "1"; else p2 = "0";
		if (ae$('aep_p3').checked == true) p3 = "1"; else p3 = "0";
		if (ae$('aep_p4').checked == true) p4 = "1"; else p4 = "0";
		if (ae$('aep_p5').checked == true) p5 = "1"; else p5 = "0";
		if (ae$('aep_p6').checked == true) p6 = "1"; else p6 = "0";
		if (ae$('aep_p7').checked == true) p7 = "1"; else p7 = "0";

		ae_cb(pin + ";" + ae$('aep_text').value,credit,nuo,iki,p1,p2,p3,p4,p5,p6,p7); // user pressed OK  
	}	
	// hide dialog layers  
	ae$('aep_ovrl').style.display = ae$('aep_ww').style.display = 'none';
}

function isUnsignedInteger(s) {
  return (s.toString().search(/^[0-9]+$/) == 0);
}

function status(n,nr)
{
    if (n == "1") {
		var answer = confirm("Ar blokuoti kortelę?")
		if (answer){
			window.location='cards.php?act=block&nr='+nr ;
		}
	} else {
		var answer = confirm("Ar atblokuoti kortelę?")
		if (answer){
			window.location='cards.php?act=unblock&nr='+nr ;
		}
	}	
}

//-->
</SCRIPT>
<table class="innercont" cellspacing="0" cellpadding="5" border="0">

<?php
if (!empty($cards)) {

$from = firstOfPrevMonth();
$to = lastOfPrevMonth();

echo "<tr><td class='leftcol'>";
?>

<script>
  $(function() {
    $( ".dt" ).datepicker({
      showOn: "button",
      buttonImage: "img/calendar.gif",
      buttonImageOnly: true,
	  dateFormat: "yy.mm.dd",
	  regional: "lt"
    });
  });
  </script>

		<form name="form" method="post" action="printcards.php"><br>	
		<table cellspacing="0" cellpadding="5" class="params">
		<tr><th colspan="2">Ataskaitos</th></tr>
		<tr><td rowspan="2" align="left" valign="top">Pasirinkite periodą:</td><td align="right" nowrap>Nuo <input class="dt" type='text' name="from" value="<?php echo $from;?>"></td></tr>
		<tr><td align="right" nowrap>Iki <input  class="dt" type='text' name="to" value="<?php echo $to;?>"></td></tr>
		<tr><td align="right" colspan="2"><input class="but bgfilter but1 nopad" name="detali" type = "submit" onclick="javascript: form.action='printcards.php';" value = "Detali ataskaita"></td></tr>
		<tr><td align="right" colspan="2"><input class="but bgfilter but1 nopad" name="detali" type = "submit" onclick="javascript: form.action='printcardsgroups.php';" value = "Detali ataskaita pagal prekių grupes"></td></tr>
		<tr><td align="right" colspan="2"><input class="but bgfilter but1 nopad" name="detalicsv" type = "submit" onclick="javascript: form.action='printcards.php';" value = "Detali ataskaita į CSV"></td></tr>
<?php
if (isset($_SESSION['discount']) && $_SESSION['discount']=='PROTOKOLAS') {
?>
<tr><td align="right" colspan="2"><input class="but bgfilter but1 nopad" name="detalicsv" type = "submit" onclick="javascript: form.action='printprotocol.php';" value = "Protokolo ataskaita"></td></tr>
<?php
}
?>
		</table>
		</form>

<?php

echo "</td><td class='rightcol'>";


?>
<table width="100%" border = "0" cellspacing="0" cellpadding="0">
<tr>
<td valign="top">
<?php
echo "<br>";
print_pages2($page,$page_size,count($cards),$cards);
?>
</td>
<td align="center" valign="top"><br>
<table cellspacing="0" cellpadding="5" class="innercont3 width100pr">
<?php

$cnt=0;
$min = ($page-1) * $page_size;
$max = $page * $page_size;

	echo "<tr><th nowrap>Kortelės nr.</th><th>Pavadinimas</th><th>Mėn. apyv.</th><th>Blokavimo priežastis</th><th>Blokuoti</th><td></td></tr>";
	foreach ($cards as $card) {
		$cnt++;
		if ($cnt > $min && $cnt <= $max) {
			echo "<tr>";
			echo "<td>".trim($card["N51_PAV"])."</td>";
			echo "<td>".@iconv('windows-1257', 'UTF-8', htmlspecialchars($card["N51_KORTA"])). "</td>";
			echo "<td style='text-align:right;'>".printCardTurnover(trim($card["N51_PAV"]))."</td>";
			echo "<td>".@iconv('windows-1257', 'UTF-8', htmlspecialchars($card["n35_pav"])) ."</td>";
			
			echo "<td>";
			if ($card["N51_VISKAS"] == "0") {
				echo "<A onclick=\"status('1','".@iconv('windows-1257', 'UTF-8', trim($card["N51_KODAS_KT"]))."');\"><img src='img/green_new.gif' alt='' title=''></A>";
			} else {
				echo "<A onclick=\"status('0','".@iconv('windows-1257', 'UTF-8', trim($card["N51_KODAS_KT"]))."');\"><img src='img/red_new.gif' alt='' title=''></A>";
			}
			echo "</td>\n";
			echo "<td><A class='card2' href='card_edit.php?uid=".@iconv('windows-1257', 'UTF-8', trim($card["N51_KODAS_KT"]))."' >Koreguoti</A>";
			echo "/<A href='card.php?code=".@iconv('windows-1257', 'UTF-8', trim($card["N51_PAV"]))."' class='card2'>Istorija</A></td></tr>";

		}
	}	
	echo "<tr><td colspan='5' style='text-align:right;'><img src='img/green_new.gif' alt='' title=''> - Aktyvi kortelė <img src='img/red_new.gif' alt='' title=''> - Blokuota kortelė</td></tr>";



?>
</table>
</td>
<td valign="top" align="right">


</td>
</tr></table>
<?php
}
?>
</td></tr>
</table>

<?php
include("footer.php");
?>